package com.sg.zhui.visitor.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.sg.zhui.visitor.R;
import com.sg.zhui.visitor.fragment.ApplicationRecordFragment;
import com.sg.zhui.visitor.fragment.HomeFragment;
import com.sg.zhui.visitor.fragment.MineFragment;
import com.sg.zhui.visitor.widget.CustomViewPager;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by chenlipeng on 2018/7/11 0011
 * describe:  首页
 */
public class VisitorMainActivity extends AppCompatActivity {
    //    @BindView(R.id.tv_titlebar_title_visitor)
//    TextView tvTitlebarTitle;
//    @BindView(R.id.vp_content)
//    CustomViewPager vpContent;
//    @BindView(R.id.view_lines)
//    View viewLines;
//    @BindView(R.id.rb_home)
//    RadioButton rbHome;
//    @BindView(R.id.rb_classify)
//    RadioButton rbClassify;
//    @BindView(R.id.rb_me)
//    RadioButton rbMe;
//    @BindView(R.id.rg_bottom_nav)
//    RadioGroup rgBottomNav;
//    @BindView(R.id.container)
//    LinearLayout container;
    private FragmentManager fragmentManager;
    private Fragment f1, f2, f4;
    private double exitTime;
    private MyAdapter adapter;

    private List<Fragment> fragments = new ArrayList<>();
    private List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        setContentView(R.layout.activity_visitor_main);
//        ButterKnife.bind(this);

//        tvTitlebarTitle.setText("首页");
//        viewLines.setBackgroundColor(Color.parseColor("#588BC9"));
//
//        rgBottomNav.setOnCheckedChangeListener(this);
//
//        fragments.add(new HomeFragment());
//        fragments.add(new ApplicationRecordFragment());
//        fragments.add(new MineFragment());
//
//        //标题数据
//        list.add("未完成");
//        list.add("已完成");
//        list.add("已退货");
//
//        adapter = new MyAdapter(getSupportFragmentManager(), list, fragments);
//        vpContent.setAdapter(adapter);
//
//        vpContent.setScanScroll(false);//添加禁止滑动代码
    }

    /**
     * 隐藏所有Fragment
     *
     * @param transaction
     */
    public void hideAllFragment(FragmentTransaction transaction) {
        if (f1 != null) {
            transaction.hide(f1);
        }
        if (f2 != null) {
            transaction.hide(f2);
        }
        if (f4 != null) {
            transaction.hide(f4);
        }
    }


//    @Override
//    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
//        switch (checkedId) {
//            //首页
//            case R.id.rb_home:
//                vpContent.setCurrentItem(0);
//                tvTitlebarTitle.setText("首页");
//
//                break;
//            //分类
//            case R.id.rb_classify:
//                vpContent.setCurrentItem(1);
//                tvTitlebarTitle.setText("申请记录");
//                break;
//            //我的
//            case R.id.rb_me:
//                vpContent.setCurrentItem(2);  tvTitlebarTitle.setText("我的");
//                break;
//        }
//    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

//    @Override
//    public void onAttachFragment(Fragment fragment) {
//        if (f1 == null && fragment instanceof CaseFragment)
//            f1 = fragment;
//        if (f2 == null && fragment instanceof BuildFragment)
//            f2 = fragment;
//        if (f4 == null && fragment instanceof MineFragment)
//            f4 = fragment;
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBusUtil.unregister(this);
    }


    public class MyAdapter extends FragmentPagerAdapter {

        private List<String> list;

        private List<Fragment> fragments = new ArrayList<>();

        private int mCurrentView = 0;

        public MyAdapter(FragmentManager fm, List<String> list, List<Fragment> fragments) {
            super(fm);
            this.list = list;
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }


        @Override
        public CharSequence getPageTitle(int position) {
            return list.get(position);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        if(f1 != null && f1.isVisible() && resultCode == CaseFragment.RESULTCode_SUCCESS){
//            f1.onActivityResult(requestCode,resultCode,data);
//        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
